/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "user_led.h"
#include "lvgl.h"
#include "lcd_spi.h"
#include "esp_log.h"
#include "lv_port_disp_template.h"
#include "nvs_flash.h"
#include "wifi_connect.h"
#include "tcp_client.h"
#include "lv_interface.h"
#ifdef CONFIG_IDF_TARGET_ESP32
#define CHIP_NAME "ESP32"
#endif

#ifdef CONFIG_IDF_TARGET_ESP32S2BETA
#define CHIP_NAME "ESP32-S2 Beta"
#endif

static void periodic_timer_callback(void *arg);
static void lvgl_time_init(void);
static void lvgl_mainloop(void *pvParameters);



static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

// void lv_example_btn_1(void)
// {
//     lv_obj_t * label;

//     lv_obj_t * btn1 = lv_btn_crealv_example_btn_1te(lv_scr_act());
//     lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
//     lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);

//     label = lv_label_create(btn1);
//     lv_label_set_text(label, "Button");
//     lv_obj_center(label);

//     lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
//     lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_ALL, NULL);
//     lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
//     lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
//     lv_obj_set_height(btn2, LV_SIZE_CONTENT);

//     label = lv_label_create(btn2);
//     lv_label_set_text(label, "Toggle");
//     lv_obj_center(label);
// }


void app_main(void)
{
    led_init();
    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    gpio_pad_select_gpio(2);
    gpio_set_direction(2, GPIO_MODE_OUTPUT);

    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    wifi_init_sta();

    lvgl_time_init();

    xTaskCreate(tcp_client_task, "tcp_client", 4096*3, NULL, 5, NULL);
    xTaskCreate(lvgl_mainloop, "lvgl_main", 4096, NULL, 6, NULL);
}

static void lvgl_time_init(void)
{

    const esp_timer_create_args_t periodic_timer_args = {
        .callback = &periodic_timer_callback,
        /* name is optional, but may help identify the timer when debugging */
        .name = "periodic"};

    esp_timer_handle_t periodic_timer;
    ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
    esp_timer_start_periodic(periodic_timer,1000);
}

static void periodic_timer_callback(void *arg)
{
    lv_tick_inc(1);
}

static void lvgl_mainloop(void *pvParameters)
{
    lv_init();
    lv_port_disp_init();
    lvgl_interface_entry();
    while(1)
    {
        vTaskDelay(10);
        lv_task_handler();
    }
}
